﻿/*----------------------------------------------------------------
// Copyright (C) 2012
// 版权所有
//
// 文件名：OrderOnline_Model.cs
// 文件功能描述：
//
//
// 创建标识：Libing 2012-05-07 13:23:03 星期一
//
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using WMS.DBUtility;

namespace WMS.SQLServerDAL
{
    public class OrderOnline_Model : WMS.IDAL.IOrderOnline_Model
    {
        #region 基本方法

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="orderOnlineModel">类型为 WMS.Model.OrderOnline_Model 的实体</param>
        /// <returns></returns>
        public bool Insert(WMS.Model.OrderOnline_Model orderOnlineModel)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.VarChar, 50),
				new SqlParameter("@ModelName", SqlDbType.VarChar, 80),
				new SqlParameter("@ModelType", SqlDbType.Int, 4),
				new SqlParameter("@Sno", SqlDbType.Int, 4)
			};
            parameters[0].Value = orderOnlineModel.ID;
            parameters[1].Value = orderOnlineModel.ModelName;
            parameters[2].Value = orderOnlineModel.ModelType;
            parameters[3].Value = orderOnlineModel.Sno;

            try
            {
                int result = DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_Model_Insert]", parameters);

                return result > 0;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="orderOnlineModel">类型为 WMS.Model.OrderOnline_Model 的实体</param>
        /// <returns></returns>
        public bool Update(WMS.Model.OrderOnline_Model orderOnlineModel)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.VarChar, 50),
				new SqlParameter("@ModelName", SqlDbType.VarChar, 80),
				new SqlParameter("@ModelType", SqlDbType.Int, 4),
				new SqlParameter("@Sno", SqlDbType.Int, 4)
			};
            parameters[0].Value = orderOnlineModel.ID;
            parameters[1].Value = orderOnlineModel.ModelName;
            parameters[2].Value = orderOnlineModel.ModelType;
            parameters[3].Value = orderOnlineModel.Sno;

            try
            {
                return DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_Model_Update]", parameters) > 0;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 最小修改实体
        /// </summary>
        /// <param name="model">类型为WMS.Model.OrderOnline_Model的实体</param>
        /// <returns></returns>
        public bool UpdateMinimal(WMS.Model.OrderOnline_Model orderOnlineModel)
        {
            SqlCommand cmd = new SqlCommand("[OrderOnline_Model_UpdateMinimal]");

            if (orderOnlineModel.ID != string.Empty)
            {
                cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar, 50)).Value = orderOnlineModel.ID;
            }

            if (orderOnlineModel.ModelName != string.Empty)
            {
                cmd.Parameters.Add(new SqlParameter("@ModelName", SqlDbType.VarChar, 80)).Value = orderOnlineModel.ModelName;
            }

            if (orderOnlineModel.ModelType != null)
            {
                cmd.Parameters.Add(new SqlParameter("@ModelType", SqlDbType.Int, 4)).Value = orderOnlineModel.ModelType;
            }

            if (orderOnlineModel.Sno != null)
            {
                cmd.Parameters.Add(new SqlParameter("@Sno", SqlDbType.Int, 4)).Value = orderOnlineModel.Sno;
            }

            try
            {
                return DbHelperSQL.ExecuteStoredProcNonQuery(cmd) > 0;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 删除实体(根据主键 ID)
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public bool DeleteByID(string id)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.VarChar,50)
			};
            parameters[0].Value = id;

            try
            {
                return DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_Model_DeleteByID]", parameters) > 0;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 删除实体(指定条件)
        /// </summary>
        /// <param name="whereClause">WHERE语句</param>
        /// <returns></returns>
        public bool DeleteDynamic(string whereClause)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@WhereClause", SqlDbType.NVarChar,2000)
			};
            parameters[0].Value = whereClause;

            try
            {
                return DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_Model_DeleteDynamic]", parameters) > 0;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 获取实体(根据主键 ID)
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public WMS.Model.OrderOnline_Model GetByID(string id)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.VarChar,50)
			};
            parameters[0].Value = id;

            try
            {
                List<WMS.Model.OrderOnline_Model> orderOnlineModelList = new List<WMS.Model.OrderOnline_Model>();

                SqlDataReader dr = DbHelperSQL.ExecuteStoredProcReader("[OrderOnline_Model_GetByID]", parameters);
                while (dr.Read())
                {
                    WMS.Model.OrderOnline_Model orderOnlineModel = new WMS.Model.OrderOnline_Model();

                    orderOnlineModel.ID = Convert.ToString(dr["ID"]);
                    orderOnlineModel.ModelName = Convert.ToString(dr["ModelName"]);
                    orderOnlineModel.ModelType = Convert.ToInt32(dr["ModelType"]);
                    orderOnlineModel.Sno = Convert.ToInt32(dr["Sno"]);
                    orderOnlineModel.ModelState = WMS.Model.Enum.ModelState.None;

                    orderOnlineModelList.Add(orderOnlineModel);
                }

                // Close the dr, which will cause the connection to be closed as well.
                dr.Close();

                if (orderOnlineModelList.Count > 0)
                {
                    return orderOnlineModelList[0];
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        #endregion

        #region	公共方法

        /// <summary>
        /// 获取实体列表(指定数目和查询条件)
        /// </summary>
        /// <param name="topClause">指定实体数</param>
        /// <param name="whereClause">WHERE语句</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Model> Get(int topClause, string whereClause)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@TopClause", SqlDbType.Int,4),
				new SqlParameter("@WhereClause", SqlDbType.NVarChar,2000)
			};
            parameters[0].Value = topClause;
            parameters[1].Value = whereClause;

            try
            {
                List<WMS.Model.OrderOnline_Model> orderOnlineModelList = new List<WMS.Model.OrderOnline_Model>();

                SqlDataReader dr = DbHelperSQL.ExecuteStoredProcReader("[OrderOnline_Model_GetDynamic]", parameters);
                while (dr.Read())
                {
                    WMS.Model.OrderOnline_Model orderOnlineModel = new WMS.Model.OrderOnline_Model();

                    orderOnlineModel.ID = Convert.ToString(dr["ID"]);
                    orderOnlineModel.ModelName = Convert.ToString(dr["ModelName"]);
                    orderOnlineModel.ModelType = Convert.ToInt32(dr["ModelType"]);
                    orderOnlineModel.Sno = Convert.ToInt32(dr["Sno"]);
                    orderOnlineModel.ModelState = WMS.Model.Enum.ModelState.None;

                    orderOnlineModelList.Add(orderOnlineModel);
                }

                // Close the dr, which will cause the connection to be closed as well.
                dr.Close();

                return orderOnlineModelList;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 获取实体列表(指定查询条件)
        /// </summary>
        /// <param name="whereClause">WHERE语句</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Model> Get(string whereClause)
        {
            return Get(0, whereClause);
        }

        /// <summary>
        /// 获取分页实体列表
        /// </summary>
        /// <param name="whereClause">WHERE 语句</param>
        /// <param name="orderByClause">ORDER BY 语句</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小(每页显示的记录数)</param>
        /// <param name="totalRecords">记录总数</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Model> GetPaged(string whereClause, string orderBy, int pageIndex, int pageSize, out int totalRecords)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@WhereClause", SqlDbType.NVarChar, 2000),
				new SqlParameter("@OrderBy", SqlDbType.NVarChar, 1000),
				new SqlParameter("@PageIndex", SqlDbType.Int, 4),
				new SqlParameter("@PageSize", SqlDbType.Int, 4)
			};
            parameters[0].Value = whereClause;
            parameters[1].Value = orderBy;
            parameters[2].Value = pageIndex;
            parameters[3].Value = pageSize;

            try
            {
                List<WMS.Model.OrderOnline_Model> orderOnlineModelList = new List<WMS.Model.OrderOnline_Model>();

                SqlDataReader dr = DbHelperSQL.ExecuteStoredProcReader("[OrderOnline_Model_GetPaged]", parameters);
                while (dr.Read())
                {
                    WMS.Model.OrderOnline_Model orderOnlineModel = new WMS.Model.OrderOnline_Model();

                    orderOnlineModel.ID = Convert.ToString(dr["ID"]);
                    orderOnlineModel.ModelName = Convert.ToString(dr["ModelName"]);
                    orderOnlineModel.ModelType = Convert.ToInt32(dr["ModelType"]);
                    orderOnlineModel.Sno = Convert.ToInt32(dr["Sno"]);
                    orderOnlineModel.ModelState = WMS.Model.Enum.ModelState.None;

                    orderOnlineModelList.Add(orderOnlineModel);
                }

                totalRecords = orderOnlineModelList.Count;

                if (dr.NextResult())
                {
                    if (dr.Read())
                    {
                        totalRecords = Convert.ToInt32(dr[0]);
                    }
                }

                // Close the dr, which will cause the connection to be closed as well.
                dr.Close();

                return orderOnlineModelList;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        #endregion

        #region 自定义方法

        /// <summary>
        /// 获取全部实体列表
        /// </summary>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Model> GetAll()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("ModelType=5");
            strSql.AppendFormat(" ORDER BY Sno");
            return Get(strSql.ToString());
        }

        /// <summary>
        /// 根据角色权限获取一级菜单
        /// </summary>
        /// <param name="rolePermission">角色权限</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Model> GetByPermission(string rolePermission)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("ModelType=5");
            strSql.AppendFormat(" AND ID IN ({0})", rolePermission);
            strSql.AppendFormat(" ORDER BY Sno");
            return Get(strSql.ToString());
        }

        #endregion
    }
}
